<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增采购')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-purchase-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">商品名称：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="cooCommodityId" name="cooCommodityId" type="hidden" th:value="${cooCommodityId}"/>
                    <!--                新增隐藏字段存储批发价-->
                    <input id="wholesalePrice" name="wholesalePrice" type="hidden"/>
                    <input class="form-control" type="text" onclick="selectCooperationCommodityIdRadio()"
                           id="commodityName"
                           readonly="true" th:value="${commodityName}" required>
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">合作公司：</label>
            <div class="col-sm-8">
                <input id="cooperationName" disabled name="cooperationName" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">采购人：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="userId" name="userId" type="hidden" th:value="${userId}"/>
                    <input class="form-control" type="text" onclick="selectUserIdRadio()"
                           id="userName"
                           readonly="true" th:value="${userName}" required>
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">数量：</label>
            <div class="col-sm-8">
                <input id="quantity" name="quantity" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">金额：</label>
            <div class="col-sm-8">
                <input id="amount" name="amount" class="form-control" type="text" required readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">运输方式：</label>
            <div class="col-sm-8">
                <select name="transportType" class="form-control m-b" th:with="type=${@dict.getType('transport_type')}"
                        required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "chain/purchase"
    var cooperationPrefix = ctx + "chain/cooperation"
    var cooperationCommodityPrefix = ctx + "chain/cooperationCommodity";
    var userPrefix = ctx + "system/user";
    $("#form-purchase-add").validate({
        focusCleanup: true
    });


    function selectCooperationCommodityIdRadio() {
        var cooperationId = $("#cooperationId").val();
        var options = {
            title: '选择商品',
            url: cooperationCommodityPrefix + "/selectCooperationCommodity",
            callBack: doCooperationCommoditySubmit
        };
        $.modal.openOptions(options);
    }

    function selectUserIdRadio() {
        var options = {
            title: '选择商品库存',
            url: userPrefix + "/selectUser?deptId=103",
            callBack: doUserSubmit
        };
        $.modal.openOptions(options);
    }


    function doCooperationCommoditySubmit(index, layer) {
        var body = $.modal.getChildFrame(index);
        console.log("弹窗数据-cooCommodityId:",body.find("#cooCommodityId").val());
        console.log("弹窗数据-wholesalePrice:",body.find("#wholesalePrice").val());
        $("#cooCommodityId").val(body.find("#cooCommodityId").val());
        $("#commodityName").val(body.find("#commodityName").val());
        $("#cooperationName").val(body.find("#cooperationName").val());
        //新增获取批发价逻辑
        $("#wholesalePrice").val(body.find("#wholesalePrice").val());

        calculateAmount();//选择商品后主动计算
        $.modal.close(index);
    }

    function doUserSubmit(index, layer) {
        var body = $.modal.getChildFrame(index);
        $("#userId").val(body.find("#userId").val());
        $("#userName").val(body.find("#userName").val());
        $.modal.close(index);
    }

    function submitHandler() {
        if ($.validate.form()) {
            if ($("#userId").val() == '') {
                $.modal.alertWarning('请选择采购人...');
                return false;
            }
            $.operate.save(prefix + "/add", $('#form-purchase-add').serialize());
        }
    }
    function calculateAmount() {
        let quantity = parseFloat($("#quantity").val().trim());
        let price = parseFloat($("#wholesalePrice").val().trim());
        $("#amount").val('');//清空错误数据

        //优先处理空输入情况
        if ($("#quantity").val().trim() === "")
            return;//空输入不提示
        // 先校验商品价格有效性
        if (isNaN(price) || price <= 0) {
            if (quantity > 0) {//仅当有输入数量时才提示
                $.modal.alertWarning('请选择有效的商品');
            }
            return;
        }

        //再校验采购数量
        if (isNaN(quantity) || quantity <= 0) {
            if ($("#quantity").val().length > 0)//仅当输入框非空时提示
                $.modal.alertWarning('请输入有效的采购数量');
            return;
        }

        $("#amount").val((quantity * price).toFixed(2));
    }
    $("#quantity").on('input propertychange',function (){
        calculateAmount();
    })
</script>
</body>
</html>